package com.vitas.coin.ui.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vitas.coin.callback.DeleteListener;
import com.vitas.coin.data.bean.ChildrenVoiceBean;
import com.vitas.coin.databinding.ItemPlaylistBinding;
import com.vitas.coin.ui.act.PlayAct;
import com.vitas.coin.util.Utils;
import com.vitas.coin.util.drag.DragSwipe;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

public class ListDetailAdapter extends RecyclerView.Adapter implements DragSwipe {

    ArrayList<ChildrenVoiceBean> list;
    LayoutInflater inflater;
    FragmentActivity fragment;
    DeleteListener callback;

    public ListDetailAdapter(FragmentActivity frag, DeleteListener callback, ArrayList list) {
        inflater = LayoutInflater.from(frag);
        this.list = list;
        fragment = frag;
        this.callback = callback;
    }

    public void setList(ArrayList list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPlaylistBinding binding = ItemPlaylistBinding.inflate(inflater, parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onItemSwapped(int fromPosition, int toPosition) {
        Log.e("tagg", "onItemSwapped " + fromPosition + ", " + toPosition);
        Collections.swap(list, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onItemDeleted(int position) {
        Log.e("tagg", "onItemDeleted " + position);
        ChildrenVoiceBean bean = list.remove(position);
        notifyItemRemoved(position);
        callback.onItemDelete(bean);
    }

    @Override
    public void onItemDone(int position) {
        Log.e("tagg", "onItemDone " + position);
        ChildrenVoiceBean bean = list.remove(position);
        notifyItemRemoved(position);
        callback.onItemDelete(bean);
    }

    class Holder extends RecyclerView.ViewHolder {
        ItemPlaylistBinding binding;

        Holder(ItemPlaylistBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            ChildrenVoiceBean item = list.get(p);
            binding.tvCount.setVisibility(ViewGroup.GONE);
            binding.tvName.setText(item.getName());
            binding.ivPlay.setVisibility(ViewGroup.VISIBLE);
            binding.ivPlay.setOnClickListener(v -> {
                callback.onItemEdit(item);
            });
            itemView.setOnClickListener(v -> {
                Intent i = new Intent(itemView.getContext(), PlayAct.class);
                i.putExtra("index", getAdapterPosition());
                i.putParcelableArrayListExtra("list", list);
                itemView.getContext().startActivity(i);
            });
            binding.set.setVisibility(ViewGroup.VISIBLE);
            binding.set.setOnClickListener(v -> {
                if (item.getVoiceUrl().startsWith("file://")) {
                    Utils.INSTANCE.setSound(fragment, new File(item.getVoiceUrl()), item.getName());
                } else {
                    Utils.INSTANCE.download(fragment, item, 2);
                }
            });
        }
    }
}
